<?php
class Osml_Controller_Action extends Jnd_Controller_Action
{

    public function init()
    {
        parent::init();
        $view = Jnd_App::getResource("view");
        
        if (Zend_Auth::getInstance()->getIdentity() == "") {
            $connect = new Front_Form_Connect();
            $view->formConnect = $connect;
            $view->connecte = false;
        } else {
            $view->connecte = true;
        }
        
        // Rss
        $news = $this->_recupeNews();
        if ($news !== false) {
            $this->view->rss_titre = $news->getTitle();
            $this->view->rss_date = $news->getDateCreated();
            $this->view->rss_message = $news->getContent();
            $this->view->rss_permalink = $news->getPermalink();
        } else {
            $this->view->rss_titre = "Hors ligne";
            $this->view->rss_date = date(Jnd_Global_Constantes::FORMAT_DATE_WEB_COURT);
            $this->view->rss_message = "mode hors connexion";
            $this->view->rss_permalink = "#";
        }
        
        // Les liens
        $model = new Back_Model_Liens();
        $liens = $model->lister("-creationDate");
        $this->view->liens = $liens;
        $this->view->afficherSidebar = true;
    }

    protected function _recupeNews()
    {
        try {
            $feed = Zend_Feed_Reader::import("http://www.ffbad.org/index.php?mduuseid=MTk%3D&dsgtypid=285&page=rss");
            $nombreNews = $feed->count();
            
            $rnd = rand(1, $nombreNews - 1);
            for( $i = 1; $i <= $rnd; $i ++ ) {
                $feed->next();
            }
            $news = $feed->current();
            
            return $news;
        }
        catch (Exception $e) {
            return false;
        }
    }
}
